<?php
/**
 * Author: DefinitlyEvil
 * Created at: 2019/9/3 22:56
 */

namespace App\Tools;


class DBHelper
{

    public static function connect() {
        return self::connect_other();
    }

    public static function connect_other($database_name = null) {
        $pdo = new \PDO(
            sprintf('mysql:host=%s;dbname=%s', getenv('DB_HOST'), $database_name === null ? getenv('DB_NAME') : $database_name),
            getenv('DB_USER'), getenv('DB_PASS')
        );
        $pdo->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
        $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
        $pdo->exec('SET names utf8');
        return $pdo;
    }

    public static function getUser($conn, $id) {
        $id = intval($id);
        $stmt = $conn->prepare('SELECT * FROM accounts WHERE `id`=:id');
        $stmt->bindParam(':id', $id);
        $stmt->execute();
        $row = $stmt->fetch(\PDO::FETCH_OBJ);
        if(!$row) return null;
        return $row;
    }

}